/**
 * 依赖 qmsk-1.1.js, pagination.js
 *
 {
 method:'post'
 url : '../couponRule/selecter',
 data : {
 shopId : shopId
 },
 heads : ["ruleId", "ruleName"],
 titles : ["优惠券Id", "优惠券名称"],
 zindex: 500, //缺省值
 callBack : callBack
 }
 */
(function() {
	var html = "<div id='commonSelecter_yinying' class='yinying' style='z-index:499;'></div>" + "<div style='z-index:500;' id='commonSelecter' class='hideDiv'>" + "<div style='float: right; margin-bottom: 5px; width: 20px; height: 20px; line-height: 20px; text-align: center; background-color: #eee; margin-bottom: 5px; border: 1px solid #ccc;'>" + "<span id='commonSelecter_close' style='cursor: pointer; font-size: 14px;'>X</span></div>" + "<div class='bottom_search'>" + "	<div class='serWrapper' >" + "<input type='text' id='commonSelecter_searchName' class='txt' >" + "<strong class='refresh' onclick='$(this).prev().val(\'\')'>清空</strong></div>" + "<input type='button' id='commonSelecter_search' style='width: auto; margin-left: 5px;' value='查询' class='common_btn'></div>" + "<table cellpadding='4' cellspacing='0' border='1' width='100%'>" + "<thead><tr id='commonSelecter_title'></tr></thead>" + "<tbody id='commonSelecter_data'></tbody></table>" + "<div id='commonSelecter_Pager' class='pager' ></div></div>";
	document.write(html);

	var shadowId = "commonSelecter_yinying";
	var panelId = "commonSelecter";

	function initSelecter(options) {

		var zindex = options.zindex;
		if (qmsk.isNotEmpty(zindex)) {
			$("#" + shadowId).css("z-index", zindex);
			$("#" + panelId).css("z-index", zindex + 1);
		}

		// 注册事件
		$("#" + panelId + "_close").unbind('click');
		$("#" + panelId + "_close").bind("click", closePanel);
		$("#" + panelId + "_search").unbind("click");
		$("#" + panelId + "_search").bind("click", search);

		//全局变量
		var cache = null;

		function openPanel() {
			$("#" + panelId + "_searchName").val('');

			var titles = options.titles;
			titles.push("操作");
			var $tr = $("#" + panelId + "_title");
			$tr.empty();

			for (var i = 0; i < titles.length; i++) {
				$tr.append("<th>" + titles[i] + "</th>");
			}
			search();
		}

		function closePanel() {
			$("#" + panelId + ",#" + panelId + "_yinying").hide();
		}

		function search() {
			var content = $("#" + panelId + "_searchName").val();
			var data = options.data;
			if (data == null) {
				data = {};
			}
			data.content = content;
			data.name = content;
			var method = options.method;
			if (qmsk.isEmpty(method)) {
				method = "get";
			}
			new Pagination({
				method : method,
				type : 'json',
				url : options.url,
				data : data,
				pageSize : 10,
				startIndex : 0,
				success : success,
				pagerHandler : panelId + "_Pager"
			});
		}

		function success(message) {
			var response = new qmsk.Response(message);
			cache = response;
			var heads = options.heads;
			var $tbody = $("#" + panelId + "_data");
			$tbody.empty();
			for (var i = 0; i < response.row; i++) {
				var $tr = $("<tr></tr>");
				for (var j = 0; j < heads.length; j++) {
					var value = response.getValue(heads[j], i);
					var $td = $("<td>" + value + "</td>");
					$tr.append($td);
				}
				var select = "<input class=\"common_btn\" type=\"button\" onclick=\"_" + panelId + "_selected(" + i + ")\" value=\"选择\"></input>";
				$tr.append($("<td>" + select + "</td>"));
				$tbody.append($tr);
			}
			$("#" + panelId + ",#" + panelId + "_yinying").show();
			addMargin($("#" + panelId));
		}

		window["_" + panelId + "_selected"] = function(row) {
			$("#" + panelId + ",#" + panelId + "_yinying").hide();
			options.callBack.call(this, cache, row);
		};

		openPanel();
	}


	window.initSelecter = initSelecter;
})();
